<template lang="">
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right" separator=">">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card>
            <el-row>
                <el-col :span="4">
                    <el-button type="primary" @click="goodsClick">添加分类</el-button>
                </el-col>
            </el-row>

            <!-- 表格 -->
            <tree-table
                :data="goodsList"
                :columns="columns"
                :selection-type="false"
                :expand-type="false"
                border
                show-index index-text="#"
                :show-row-hover="false">
                <!-- 是否有效插槽 -->
                <template slot="isok" slot-scope="scope">
                    <i class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color: lightgreen;"></i>
                    <i class="el-icon-error" v-else styel="color: red;"></i>
                </template>
                <!-- 排序插槽 -->
                <template slot="sort" slot-scope="scope">
                    <el-tag v-if="scope.row.cat_level===0">一级</el-tag>
                    <el-tag type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
                    <el-tag type="warning" v-else>三级</el-tag>
                </template>
                <!-- 操作插槽 -->
                <template slot="operate" slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="amendGoods(scope.row.cat_id)">编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGoods(scope.row.cat_id)">删除</el-button>
                </template>
            </tree-table>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="goodsData.pagenum"
                :page-sizes="[5,10,15]"
                :page-size="goodsData.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加分类对话框 -->
        <el-dialog
            title="添加分类"
            :visible.sync="dialogVisible"
            width="50%"
            @close="handleClose">
            <el-form :model="addGoodsForm" :rules="rules" ref="ruleGoodsForm" label-width="100px">
                <el-form-item label="分类名称：" prop="cat_name">
                    <el-input v-model="addGoodsForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类：" >
                    <el-cascader
                        clearable
                        v-model="selectKeys"
                        :options="parentList"
                        :props="cascadeProps"
                        expand-trigger="hover"
                        change-on-select
                        @change="parentCateChange"></el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCate">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑对话框 -->
        <el-dialog
            title="修改分类名称"
            :visible.sync="amendGoodsVisible"
            width="30%">
            <el-form :model="dateGoodsForm" :rules="dateGoodsRules" ref="dateGoodesRef" label-width="100px">
                <el-form-item label="分类名称：" prop="cat_name">
                    <el-input v-model="dateGoodsForm.cat_name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button @click="amendGoodsVisible = false">取 消</el-button>
                <el-button type="primary" @click="dateGoodsOk">确 认</el-button>
            </span>
        </el-dialog>
        
    </div>
</template>
<script>
export default {
    data() {
        return {
            goodsData: {
                type: [3],
                pagenum: 1,
                pagesize: 5
            },
            goodsList: [],
            total: 0,
            columns: [
                {
                    label: '分类名称',
                    prop: 'cat_name'
                },
                {
                    label: '是否有效',
                    // 定义当前列为模板列
                    type: 'template',
                    // 表示当前列使用的模板名称
                    template: 'isok'
                },
                {
                    label: '排序',
                    type: 'template',
                    template: 'sort'
                },
                {
                    label: '操作',
                    type: 'template',
                    template: 'operate'
                },
            ],
            dialogVisible: false,
            amendGoodsVisible: false,
            addGoodsForm: {
                cat_name: '',
                // 父级分类id
                cat_pid: 0,
                cat_level: 0
            },
            dateGoodsForm: {},
            // 验证规则
            rules: {
                cat_name: [
                    {required: true, message:'请输入分类名称', trigger: 'blur'}
                ]
            },
            dateGoodsRules: {
                cat_name: [
                    {required: true, message:'请输入分类名称', trigger: 'blur'}
                ]
            },
            parentList:[],
            cascadeProps: {
                value: 'cat_id',
                label: 'cat_name',
                children: 'children'
            },
            selectKeys: []
        }
    },
    created() {
        this.goodsCateList()
    },
    methods: {
        async goodsCateList() {
            const {data:res} = await this.$http.get('/categories',{params: this.goodsData})
            if(res.meta.status!==200){
                return this.$message.error('获取商品列表失败')
            }
            this.goodsList = res.data.result,
            this.total = res.data.total
        },
        handleSizeChange(newSize) {
            this.goodsData.pagesize = newSize
            this.goodsCateList()
        },
        handleCurrentChange(newPag) {
            this.goodsData.pagenum = newPag
            this.goodsCateList()
        },
        async goodsClick() {
            // const {data:res} = await this.$http.post('/categories', this.addGoodsForm)
            this.getparentCateList()
            this.dialogVisible = true
        },
        async getparentCateList() {
            const {data:res} = await this.$http.get('/categories', {params:{type:2}})
            if(res.meta.status!==200){
                this.$message.error('获取父级分类列表失败！')
            }
            console.log(res.data)
            this.parentList = res.data
        },
        parentCateChange() {
            console.log(this.selectKeys)
            if(this.selectKeys.length>0){
                this.addGoodsForm.cat_pid = this.selectKeys[this.selectKeys.length-1]
                // 为当前分类等级复制   
                this.addGoodsForm.cat_level = this.selectKeys.length
                return
            }else{
                this.addGoodsForm.cat_pid=0
                this.addGoodsForm.cat_level=0

            }
        },
        handleClose() {
            // 关闭对话框重置表单
            this.$refs.ruleGoodsForm.resetFields()
            this.selectKeys = []
            this.addGoodsForm.cat_level=0
            this.addGoodsForm.cat_pid=0
        },
        addCate() {
            this.$refs.ruleGoodsForm.validate(async valid=>{
                if(!valid) return
                const {data:res} = await this.$http.post('/categories', this.addGoodsForm)
                if(res.meta.status !==201){
                    this.$message.error('添加失败')
                    return
                }
                this.$message.success('添加成功')
                this.goodsCateList()
                this.dialogVisible = false
            }) 
        },
        async amendGoods(id) {
            const {data:res} = await this.$http.get('/categories/'+id)
            if(res.meta.status!==200){
                this.$message.error('查询失败')
            }
            this.dateGoodsForm = res.data
            this.amendGoodsVisible = true
        },
        dateGoodsOk() {
            this.$refs.dateGoodesRef.validate(async valid=>{
                if(!valid) return
                const {data:res} = await this.$http.put('/categories/'+this.dateGoodsForm.cat_id,{cat_name:this.dateGoodsForm.cat_name})

                if(res.meta.status!==200) {
                    this.$message.error('修改分类名称失败')
                    return
                }
                this.$message.success('修改分类名称成功')
                this.amendGoodsVisible = false
                this.goodsCateList()
            })
        },
        async deleteGoods (id) {
            const confirmUser = await this.$confirm('此操作将永久删除分类，是否继续？', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
                }).catch(err => err)
                // 点击取消返回 cancel ，点击确认返回 confirm 
                if(confirmUser!=='confirm') {
                return this.$message.info('取消删除')
            }
            const {data:res} = await this.$http.delete('/categories/'+id)
            if(res.meta.status!==200) {
                this.$message.error('删除分类失败')
                return
            }
            this.$message.success('删除成功')
            this.goodsCateList()
        }
    }
}
</script>
<style lang="less">
.el-cascader{
    width:100%
}
</style>